﻿<%@ Page Title="" Language="C#" MasterPageFile="~/master/Attendance.master" AutoEventWireup="true" CodeFile="ReportMgmt.aspx.cs" Inherits="LDAP.Web.View.Attendance.ReportMgmt" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <link rel="stylesheet" href="<%= StaticDomain %>/scripts/util/ui.uapselector/skin/default/main.css?r=<%= R %>" />
    <link rel="stylesheet" href="<%= StaticDomain %>/scripts/util/ui_card/ui_card.css?r=<%= R %>" />
    <link rel="stylesheet" href="<%= StaticDomain %>/scripts/util/JqModal/JqPopU.css?r=<%= R %>" />
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <style type="text/css">
        #selectable .ui-selecting { background: #FECA40; }
        #selectable .ui-selected { background: #F39814; color: white; }
        #selectable { list-style-type: none; margin: 0; padding: 0; }
        #selectable li { margin: 3px; padding: 0.4em; font-size: 1.2em; }

        .navbar-form .form-group {
            vertical-align: top;
        }
        .niceTab th,.niceTab td {
            line-height:24px;
        }
        .niceTab td {
            font-size: 12px;
            color:black;
            
        }
        .niceTab thead td {
            text-align:left;
        }
        .niceTab tfoot td {
            text-align:left;
        }
    </style>
    
    <script type="text/javascript" src="<%= StaticDomain %>/scripts/util/My97DatePicker/WdatePicker.js"></script>
    <style type="text/css">
        .Wdate {
            height:34px;
            width: 150px;
            display:inline-block;
        }
    </style>

    <script type="text/javascript" src="<%= StaticDomain %>/scripts/util/ui.uapselector/jquery.ui.uapselector.js?r=<%= R %>"></script>
    <script type="text/javascript" src="<%= StaticDomain %>/scripts/util/ui_card/FixImgSize.js"></script>
    <script type="text/javascript" src="<%= StaticDomain %>/scripts/util/template/trimpath-template.js"></script>
    <script type="text/javascript" src="<%= StaticDomain %>/scripts/util/JqModal/JqPopU.js?r=<%= R %>"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">    
    <div class="alert alert-success" style="display:none">正在努力获取考勤数据ing</div>
    <div>
        <table class="niceTab" cellspacing="0" cellpadding="0" border="0">
            <thead>
                <tr><th colspan="18"><h2 style="margin:10px 0;">考勤统计表</h2></th></tr>
                <tr>
                    <th colspan="2">部门</th>
                    <td colspan="12"><span id="angel"></span></td>
                    <th colspan="2">考勤时间</th>
                    <td colspan="2">
                        <input type="text" class="form-control Wdate" id="month" onfocus="WdatePicker({skin:'whyGreen',dateFmt:'yyyy年MM月',maxDate:'<%= DateTime.Today.Year %>-<%= DateTime.Today.Month.ToString().PadLeft(2,'0') %>-01'})" />
                        <button class="btn btn-default btn-info" type="button" id="add">确定</button>
                    </td>
                </tr>
                <tr>
                    <th>序号</th>
                    <th>员工号</th>
                    <th>姓名</th>
                    <th style="color:red">入职日期</th>
                    <th style="color:red">排班天数</th>
                    <th>上班天数</th>
                    <th>病假天数</th>
                    <th>事假天数</th>
                    <th>年假天数</th>
                    <th>其他带薪假天数</th>
                    <th>周末值班天数</th>
                    <th>公假值班天数</th>
                    <th style="color:red">中班天数</th>
                    <th style="color:red">夜班天数</th>
                    <th>迟早、早退次数</th>
                    <th>旷工天数</th>
                    <th>备注</th>
                    <th>员工确认</th>
                </tr>
            </thead>
            <tbody id="userList">
                <tr>
                    <td>&nbsp;</td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr> 
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="16" style="text-align:left;">
                        <pre style="border:none;background:none; margin:0;">备注：
1：排班天数指一个月内应该上班的天数（即周一到周五的天数总和，有公休假时要扣除公休假），新员工和离职人员上班天数少于21.75的，排班按21.75计算，多于21.75的排班天数和其他员工计算方式相同。
2：上班天数指员工本月实际上班的天数
3：病假和事假和年假天数根据假期申请表上的记录填写，必须有相关权限人签字批准才生效，不得考勤结束以后补假。
4：其他带薪假指丧假、产假、婚假等公司规定的有薪假，产假和婚假统计时由于包含了周末，所以排班天数和其他员工应该不同，请注意。
5：周末值班天数指需要付工资的在周末值班的天数，如安排调休的不要记入周末值班天数中，在值班申请单备注里注明就可以了
6：公休假加班指元旦（1.1）、春节（除夕到初二）、清明节（4.4）、五一节（5.1）、端午节(农历五月初五）、中秋节（农历八月十五）和国庆节（10.1-10.3）里值班的天数
7：中班和夜班只有论坛管理员要统计，其他部门均不统计
8：迟到早退次数请按公司员工手册规定做好统计
9：旷工指没有请假又没有来上班的天数，旷工是非常严重的行为，可能导致解雇，所以请务必了解清楚
10：如有其他需要说明实现请写在备注栏</pre>
                    </td>
                    <td colspan="2">统计人：<br /><br />批准人：<br /><br />日期：<br /><br /></td>
                </tr>
            </tfoot>
        </table>
    </div>

    <textarea name="userItem" id="userItem" style="display:none">        
        {for o in list}
            <tr id="record_@{o.Id}">
                <td>@{page.rowCount++}</td>
                <td>@{o.Id}</td>
                <td>@{o.UserName}</td>
                <td>&nbsp;</td>
                <td>@{workdays}</td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td style="text-align:left;"></td>
                <td>&nbsp;</td>
            </tr> 
        {/for}
    </textarea>

    <script type="text/javascript">
        $(document).ready(function(){
            page.userControl = new CYUAP.Selector({ divID: "angel", typeID: 2, totalSelectNum: 100,  showMoreBtn:true, width: Math.max(document.documentElement.clientWidth * 0.83-500, 500), searchData: page.allDept });
            $("#add").click(page.generalReport);
        });

        var page = {
            handler: "/handler/attendanceMgmt.ashx?action=",
            rowCount: 0,

            generalReport: function () {
                var date = $("#month").val().match(/\d+/g);
                if (date == null || date.length != 2) {
                    alert("月份没选");
                    return false;
                }

                var depts = page.userControl.getValue();
                if (depts.length == 0) {
                    alert("请选择部分");
                    return false;
                }
                var str = [];
                $.each(depts, function (i, q) {
                    str.push(q.name);
                });

                $(".alert-success").show();
                $.post(page.handler + "generalReport", { year: date[0], month: date[1], depts: str.join(',') }, function (data) {
                    if (data.code == 0) {
                        alert(data.msg);
                        return false;
                    }

                    page.rowCount = 1;
                    $("#userList").html(TrimPath.processDOMTemplate("userItem", data));

                    page.weekend = data.weekend.split(',');

                    $.each(data.formList, function (i, q) {
                        q.ExceptionEndDate = q.ExceptionEndDate.ToDate();
                        q.ExceptionStartDate = q.ExceptionStartDate.ToDate();
                    });

                    var user = ''; var flag = false;
                    var schedule; var special = []; var hasRecord = [];
                    $.each(data.recordList, function (i, q) {
                        if (q.ygbh != user) {
                            user = q.ygbh;
                            flag = true;
                            hasRecord = [];
                            schedule = null;
                            $.each(data.scheduleList, function (ii, qq) {
                                if (qq.Id == user) {
                                    schedule = qq;
                                    return false;
                                }
                            });

                            special = [];
                            $.each(data.specialList, function (ii, qq) {
                                if (qq.UserId == user) {
                                    special.push(qq);
                                }
                            });
                        }
                        else {
                            flag = false;
                        }
                        var d = new Date(q.date + " 00:00:00");
                        if (!flag || (d.getMonth() == parseInt(date[1], 10) - 1)) {
                            hasRecord.push(d.getDate());
                            var lmax = null;
                            if (i != 0 && (d - new Date(data.recordList[i - 1].date + " 00:00:00") == 86400000)) {
                                lmax = data.recordList[i - 1].max.split(':')[0];
                            }
                            var ooooo;
                            var type = showSchedule(q.date, special, schedule);
                            ////console.log(q.date +" "+ schedule +"  "+ special.length);
                            type = type.type;
                            var normal = true;
                            var isWeekend = page.isWeekend(q.date);
                            var exceptionMsg = [];
                            var morning = showAttendanceStatus(q.min, 1, type, lmax, isWeekend);
                            //console.log(q.min +" " + type +" " + lmax +" "+ isWeekend+"  " + morning);
                            if (morning != "休日出勤" && morning != "延时" && morning != "") {
                                exceptionMsg.push(morning + ": " + q.date.substring(5, 10) + " " + q.min.substring(0, 5));
                                normal = false;
                            }
                            if (normal) {
                                var afternoon = showAttendanceStatus(q.max, 2, type, lmax, isWeekend, q.min);
                                //console.log(q.max +" " + type +" " + lmax +" "+ isWeekend+"  " + q.min +"  " + afternoon);
                                if (afternoon != "休日出勤" && afternoon != "延时" && afternoon != "") {
                                    exceptionMsg.push(afternoon + ": " + q.date.substring(5, 10) + " " + q.max.substring(0, 5));
                                    normal = false;
                                }
                            }
                            var addNormalDay = true;
                            if (!normal) {
                                $.each(data.formList, function (ii, qq) {
                                    //考勤异常
                                    if (qq.UserId == user && (qq.ExceptionEndDate - d >= 0) && (d - qq.ExceptionStartDate >= 0)) {
                                        var ooooo = null;
                                        switch (qq.Type) {
                                            case 2:
                                                addNormalDay = false;
                                                ooooo = $("#record_" + user).find("td:eq(7)");
                                                break;
                                            case 3:
                                                addNormalDay = false;
                                                ooooo = $("#record_" + user).find("td:eq(6)");
                                                break;
                                            case 6:
                                            case 7:
                                                addNormalDay = false;
                                                ooooo = $("#record_" + user).find("td:eq(8)");
                                                break;
                                            case 4:
                                            case 5:
                                                addNormalDay = false;
                                                ooooo = $("#record_" + user).find("td:eq(9)");
                                                break;
                                            default:
                                                break;
                                        }
                                        if (ooooo != null)
                                            $(ooooo).html((ooooo.html() == "" ? 0.0 : parseFloat(ooooo.html())) + qq.Duration);

                                        if (qq.Type == 1 || qq.Type == 8) {
                                            $("#record_" + user).find("td:eq(16)").append(showTypeName(qq.Type) + ": " + q.date.substring(5, 10) + "<br />");
                                        }

                                        normal = true;
                                        return false;
                                    }
                                });
                            }
                            //正常考勤
                            if (normal && !isWeekend) {
                                if (addNormalDay) {
                                    ooooo = $("#record_" + user).find("td:eq(5)");
                                    $(ooooo).html((ooooo.html() == "" ? 0 : parseFloat(ooooo.html())) + 1);
                                }
                            }
                                //周末
                            else if (normal && isWeekend) {
                                var outOfDuty = null;
                                $.each(data.formList, function (ii, qq) {
                                    if (qq.UserId == user && (qq.ExceptionEndDate - q.date >= 0) && (q.date - qq.ExceptionStartDate >= 0)) {
                                        outOfDuty = qq;
                                        return false;
                                    }
                                });

                                if (outOfDuty != null) {
                                    var ooooo = null;
                                    if (outOfDuty.Type == 10) {
                                        ooooo = $("#record_" + user).find("td:eq(10)");
                                    }
                                    else if (outOfDuty.Type == 11) {
                                        ooooo = $("#record_" + user).find("td:eq(11)");
                                    }
                                    if (ooooo != null)
                                        $(ooooo).html((ooooo.html() == "" ? 0 : parseFloat(ooooo.html())) + outOfDuty.Duration);
                                }
                            }
                            else if (!normal && !isWeekend) {
                                var tempDay = 1.0; var lateOrLeave = 0;
                                $.each(exceptionMsg, function (i, q) {
                                    if (q.indexOf("旷工") > -1)
                                        tempDay -= 0.5;
                                    if (q.indexOf("早退") > -1 || q.indexOf("迟到") > -1)
                                        lateOrLeave++;
                                });
                                ooooo = $("#record_" + user).find("td:eq(5)");
                                $(ooooo).html((ooooo.html() == "" ? 0 : parseFloat(ooooo.html())) + tempDay);

                                if (tempDay < 1) {
                                    ooooo = $("#record_" + user).find("td:eq(15)");
                                    $(ooooo).html((ooooo.html() == "" ? 0 : parseFloat(ooooo.html())) + (1 - tempDay));
                                }

                                if (lateOrLeave > 0) {
                                    ooooo = $("#record_" + user).find("td:eq(14)");
                                    $(ooooo).html((ooooo.html() == "" ? 0 : parseFloat(ooooo.html())) + lateOrLeave);
                                }

                                $("#record_" + user).find("td:eq(16)").append(exceptionMsg.join('<br />') + '<br />');
                            }
                        }


                        if (i == data.recordList.length - 1 || (data.recordList[i + 1].ygbh != user && user != "")) {
                            //缺勤计算
                            var noRecord = page.calcNoRecord(data.maxDay, hasRecord);
                            $.each(noRecord, function (ii, qq) {
                                var normal = false;
                                var addNormalDay = true;
                                $.each(data.formList, function (ii, qq) {
                                    //考勤异常
                                    if (qq.UserId == user && (qq.ExceptionEndDate - d >= 0) && (d - qq.ExceptionStartDate >= 0)) {
                                        var ooooo = null;
                                        switch (qq.Type) {
                                            case 2:
                                                addNormalDay = false;
                                                ooooo = $("#record_" + user).find("td:eq(7)");
                                                break;
                                            case 3:
                                                addNormalDay = false;
                                                ooooo = $("#record_" + user).find("td:eq(6)");
                                                break;
                                            case 6:
                                            case 7:
                                                addNormalDay = false;
                                                ooooo = $("#record_" + user).find("td:eq(8)");
                                                break;
                                            case 4:
                                            case 5:
                                                addNormalDay = false;
                                                ooooo = $("#record_" + user).find("td:eq(9)");
                                                break;
                                            default:
                                                break;
                                        }
                                        if (ooooo != null)
                                            $(ooooo).html((ooooo.html() == "" ? 0.0 : parseFloat(ooooo.html())) + qq.Duration);

                                        if (qq.Type == 1 || qq.Type == 8) {
                                            $("#record_" + user).find("td:eq(16)").append(showTypeName(qq.Type) + ": " + q.date.substring(5, 10) + "<br />");
                                        }
                                        normal = true;
                                        return false;
                                    }
                                });

                                if (normal) {
                                    if (addNormalDay) {
                                        var ooooo = $("#record_" + user).find("td:eq(5)");
                                        $(ooooo).html((ooooo.html() == "" ? 0 : parseFloat(ooooo.html())) + 1);
                                    }
                                }
                                else {
                                    $("#record_" + user).find("td:eq(16)").append("缺勤: " + date[1]+"/"+ qq + "<br />");
                                    var ooooo = $("#record_" + user).find("td:eq(15)");
                                    $(ooooo).html((ooooo.html() == "" ? 0 : parseFloat(ooooo.html())) + 1.0);
                                }
                            });
                        }
                    });
                                        
                    $(".alert-success").hide();
                }, "json");
            },

            isWeekend: function (date) {
                var day = date.split('/')[2];
                var exists = false;
                $.each(page.weekend, function (i, q) {
                    if (q == parseInt(day, 10)) {
                        exists = true;
                        return false;
                    }
                });
                return exists;
            },

            calcNoRecord: function (maxDay, hasRecord) {
                var noRecord = [];

                for (var i = 1; i <= maxDay; i++) {
                    var containsRecord = false;

                    $.each(page.weekend, function (ii, qq) {
                        if (qq == i) {
                            containsRecord = true;
                            return false;
                        }
                    });
                    if (!containsRecord) {
                        $.each(hasRecord, function (ii, qq) {
                            if (qq == i) {
                                containsRecord = true;
                                return false;
                            }
                        });
                    }
                    if (!containsRecord)
                        noRecord.push(i);
                }
                return noRecord;
            }
        };

        page.allDept = <%= ToJson(DeptList).ToLower() %>;
    </script>
</asp:Content>